home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 532 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.8 KB

  1. Path: qcd.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Is this ok: *pointer++ = value ??
  5. Date: 6 Jan 1996 19:12:31 GMT
  6. Organization: Los Alamos National Laboratory
  7. Distribution: world
  8. Message-ID: <4cmhj0$irn@newshost.lanl.gov>
  9. References: <4cklvv$nmm@alcor.usc.edu>
  10. NNTP-Posting-Host: qcd.lanl.gov
  11.  
  12. In article <4cklvv$nmm@alcor.usc.edu>, wawda@alcor.usc.edu (Abu Wawda)
  13. writes: 
  14. |> I tried the following on my C compiler and it worked as expected:
  15. |> 
  16. |>     int i;
  17. |>     char *pointer;
  18. |>     
  19. |>     pointer = (char *) malloc(50);
  20. |>     for (i=0; i<50; i++) *pointer++ = i;
  21. |> 
  22. |> My only problem is why? I mean you aren't allowed to do:
  23. |> 
  24. |>     int i,p;
  25. |> 
  26. |>     for (i=0; i<50; i++) p++ = i;
  27. |> 
  28. |> Like you aren't supposed to be put anything like x+5 or some other
  29. |> unsolved quantity on the left side for assignment. However, is this an
  30.  
  31. Hi Abu, 
  32.  
  33. Your problem is that you are trying to oversimplify. I will try to address
  34. what I think is the basic misunderstanding in your statement: you think of
  35. `x+5' as an `unsolved' quantity. There is no such concept in C. 
  36.  
  37. In C, there are identifiers (i.e. names, like `x') which start of
  38. standing for for objects. Thus, when one writes &x one is talking
  39. about the address of x (naturally, this address is not an object). A point
  40. to be noted, though irrelevant to our discussion, is that
  41. not every `object' has an address: register objects have no address, and
  42. functions are not objects but have addresses.
  43.  
  44. Similarly when one says x = 5, one wants to store 5 into the object x. 5 = 3
  45. is however meaningless because it wants to store 3 into the `5', but `5' is
  46. not an object. 
  47.  
  48. However, one can also use x in a slightly different way. When one writes x +
  49. 3 or y = x, one is using x as a value. In C, there are general rules of how
  50. to determine the `value' (sometimes called `rvalue') of an object (often
  51. called `lvalue'): for non-array objects, it is the value last stored in the
  52. object. Again one sees how to interpret x+3: the `values' of x and 3 are
  53. added to produce a new value. Thus x+3 = 5 does not make sense either: this
  54. would try to store 5 into x+3, which is a value and not an object.
  55.  
  56. The ++ operator is somewhat special. It treats its operand both as a value
  57. and as an object. Thus, when one writes p++, the `object p' gets the value
  58. which is one more than the original `value p'. In addition, `p++' itself can
  59. work as a value: the value of `p++' is the original value of p (i.e. the
  60. value before the new value was stored). But note that p++ is not itself an
  61. object: it is merely a value, and hence p++ = i is wrong.
  62.  
  63. On the other hand, pointers are objects that store addresses of other
  64. objects. Thus the value of a pointer object is the address of another object.
  65. Applying the * operator to it produces this object. Thus, when you write
  66. *pointer++, the compiler parses (`understands') the expression as follows:
  67.  
  68. "Take the value of the object pointer, add one to it, and store the result
  69. into the object called pointer (this is the side effect of the ++ operator
  70. mentioned above). 
  71.  
  72. The expression pointer++ however stands for the unincremented value of the
  73. object pointer: the object that this value points to is the object that
  74. *pointer++ stands for". 
  75.  
  76. Again, as a side note, one must point out that when exactly the side effect
  77. (i.e. changing the object p) takes place is not specified by the language:
  78. the programmer should only assume that it happens by the next sequence point.
  79. The FAQ explains where the sequence points are: in your example, there is one
  80. at the end of the statement.  
  81.  
  82. Thus, even though pointer++ is not an object, *pointer++ is. This object may
  83. in turn be converted to a value if required (as in the expression *pointer++
  84. + 3). However, it may naturally be used directly as an object: *pointer++ =
  85. 3. 
  86.  
  87. Does this clarify?
  88.  
  89. |> exception for pointers? I can understand doing it backwards as
  90. |> perfectly legal:
  91. |> 
  92. |>     int x;
  93. |> 
  94. |>     x = *pointer++;
  95. |> 
  96. |> but not:
  97. |> 
  98. |>     *pointer++ = x;
  99. |> 
  100. |> I would appreciate any hints. Thanks you.
  101.  
  102. Both are legal (provided of course that pointer points to an object of
  103. appropriate type). As you can see the exception is not for pointers per se;
  104. in C however, a non-trivial expression stands for an object only when dealing
  105. with pointers. C++ (a related, but completely different language) rules are
  106. different: many more expressions (most notably ++p etc.) stand for objects
  107. there. 
  108.  
  109. Cheers
  110. Tanmoy
  111. -- 
  112. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  113. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  114. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  115. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  116. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  117. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  118.